package com.czy.owner.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.entity.MaintenanceManual;

import cn.zhouchaoyuan.excelpanel.BaseExcelPanelAdapter;

/**
 * Created by King on 2017-05-16
 */

public class MaintenanceManualAdapter extends BaseExcelPanelAdapter<MaintenanceManual.TimeMileageBean, MaintenanceManual.ModelsBean, MaintenanceManual.PointBean> {
    private Context context;
    private View.OnClickListener blockListener;

    public MaintenanceManualAdapter(Context context) {
        super(context);
    }

    public MaintenanceManualAdapter(Context context, View.OnClickListener blockListener) {
        super(context);
        this.context = context;
        this.blockListener = blockListener;
    }

    // Cell
    @Override
    public RecyclerView.ViewHolder onCreateCellViewHolder(ViewGroup parent, int viewType) {
        return new CellHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_manual_cell, parent, false));
    }

    @Override
    public void onBindCellViewHolder(RecyclerView.ViewHolder holder, int verticalPosition, int horizontalPosition) {
        MaintenanceManual.PointBean manualPoint = getMajorItem(verticalPosition, horizontalPosition);
        if (null == holder || !(holder instanceof CellHolder) || manualPoint == null) {
            return;
        }
        CellHolder viewHolder = (CellHolder) holder;
        viewHolder.ivRequired.setTag(manualPoint);


            if("true".equals(manualPoint.isEmpty())){
                viewHolder.ivRequired.setVisibility(View.VISIBLE);
                viewHolder.ivRequired.setImageResource(R.mipmap.icon_manual_change_gray);
            }else if ("false".equals(manualPoint.isEmpty())){
                viewHolder.ivRequired.setVisibility(View.VISIBLE);
                viewHolder.ivRequired.setImageResource(R.mipmap.icon_manual_change);
                viewHolder.ivRequired.setOnClickListener(blockListener);
            }
            else {
                viewHolder.ivRequired.setVisibility(View.GONE);
            }


    }

    static class CellHolder extends RecyclerView.ViewHolder {

        public final ImageView ivRequired;

        public CellHolder(View itemView) {
            super(itemView);
            ivRequired = (ImageView) itemView.findViewById(R.id.iv_manual_cell_required);
        }
    }


    // Top

    @Override
    public RecyclerView.ViewHolder onCreateTopViewHolder(ViewGroup parent, int viewType) {
        return new TopHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_manual_top, parent, false));
    }

    @Override
    public void onBindTopViewHolder(RecyclerView.ViewHolder holder, int position) {
        MaintenanceManual.TimeMileageBean manualPoint = getTopItem(position);
        if (null == holder || !(holder instanceof TopHolder) || manualPoint == null) {
            return;
        }
        TopHolder viewHolder = (TopHolder) holder;
        viewHolder.tvMileage.setText("" + manualPoint.getMileage() + "km");
        viewHolder.tvPeriodic.setText("" + manualPoint.getTime() + "个月");
    }

    static class TopHolder extends RecyclerView.ViewHolder {

        public final TextView tvMileage;
        public final TextView tvPeriodic;

        public TopHolder(View itemView) {
            super(itemView);
            tvMileage = (TextView) itemView.findViewById(R.id.tv_manual_top_mileage);
            tvPeriodic = (TextView) itemView.findViewById(R.id.tv_manual_top_periodic);
        }
    }

    // Left
    @Override
    public RecyclerView.ViewHolder onCreateLeftViewHolder(ViewGroup parent, int viewType) {
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_manual_left, parent, false);
        LeftHolder leftHolder = new LeftHolder(layout);
        return leftHolder;
    }

    @Override
    public void onBindLeftViewHolder(RecyclerView.ViewHolder holder, int position) {
        MaintenanceManual.ModelsBean modelsBean = getLeftItem(position);
        if (null == holder || !(holder instanceof LeftHolder)) {
            return;
        }
        LeftHolder viewHolder = (LeftHolder) holder;
        viewHolder.tvName.setText(modelsBean.getModelsName());
    }

    static class LeftHolder extends RecyclerView.ViewHolder {

        public final TextView tvName;

        public LeftHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_manual_left_name);
        }
    }

    //=========================================left-top cell===========================================
    @Override
    public View onCreateTopLeftView() {
        return LayoutInflater.from(context).inflate(R.layout.item_manual_top_left, null);
    }
}
